﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using WinnerCMS.BLL.PageFactory.ManageHtml;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_Goods 的摘要说明。
    /// </summary>
    public partial class GoodsManage
    {
        private static GoodsDal _myDAL;


        public GoodsManage()
        {
            _myDAL = new GoodsDal();
        }

        #region 是否存在

        /// <summary>
        /// 是否存在
        /// </summary>
        public bool Exists(string contentTitle)
        {
            return _myDAL.Exists(contentTitle);
        }

        #endregion

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        public Result Add(Goods model, string tempGuid, IList<string> fields, IList<object> values)
        {
            Result resu = new Result();
            if (model.ContentTitle.Length == 0)
            {
                resu.Errors = "“货物名称”不能为空！";
                return resu;
            }
            if (model.GoodsNum.Length == 0)
            {
                resu.Errors = "“商品编号”不能为空！";
                return resu;
            }
            if (model.Content.Length == 0)
            {
                resu.Errors = "“内容”不能为空！";
                return resu;
            }
            if (model.Unit.Length == 0)
            {
                resu.Errors = "“单位”不能为空！";
                return resu;
            }

            if (model.TemplateFileName.Length == 0)
            {
                resu.Errors = "“模板”不能为空！";
                return resu;
            }
            if (string.IsNullOrEmpty(model.Brief))
            {
                model.Brief = MyString.Left(HTML.NoHtml(model.Content), 255);
            }


            if (model.Id == 0)
            {
                resu.Id = _myDAL.Add(model);

                if (tempGuid != null)
                    new PhotoListManage().FinishAction(model.Id, tempGuid);

                Result G = new GoodsCustomFieldValuesManage().Add(model.Id, fields, values);
                if (string.IsNullOrEmpty(G.Errors) == false)
                {
                    resu.Errors = G.Errors;
                }
            }
            else
            {
                resu.Id = model.Id;
                _myDAL.Edit(model);

                Result G = new GoodsCustomFieldValuesManage().Edit(model.Id, fields, values);
                if (string.IsNullOrEmpty(G.Errors) == false)
                {
                    resu.Errors = G.Errors;
                }
            }
            return resu;
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        public Result Delete(string ids)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ids))
            {
                res.Errors = "至少选择一项。";
            }
            else
            {
                _myDAL.Delete(MyConvert.ConvertToArr(ids));
            }
            return res;
        }

        #endregion

        #region 加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        public Goods GetInfo(int id)
        {
            Goods model = _myDAL.GetInfo(id);
            if (model == null)
            {
                return null;
            }
            model.Link = PageFactory.StaticFilePath.ContentShowUrl(model);
            return model;
        }

        #endregion

        /// <summary>
        /// 从栏目读取品牌数据
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IList<Goods> CountBrandWhithCategoryId(int categoryId)
        {
            return _myDAL.CountBrandWhithCategoryId(categoryId);
        }
    }
}